import axios from "axios";

const service = axios.create({
  baseURL: import .meta.env.VITE_BASE_URL,
  timeout: 5000,
});


// 请求拦截
service.interceptors.request.use(
  (config) => {
    const token = ''
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config
  },
  (error) => {
    return Promise.reject(error);
  },
);

// 响应拦截
service.interceptors.response.use(
  (response: AxiosRes) => {
    // status 是http返回的状态码
    // data 是服务器返回的数据，规范为{code, data, message}
    // 为了便于区分，将服务器返回数据重命名为responseData
    const {status, data: responseData} = response;
    if (status >= 200 && status < 300) {
      return responseData;
    } else {
      return Promise.reject(new Error(responseData.message || 'Error'));
    }
  },
  (error) => {
    return Promise.reject(error);
  },
);


export default service